package com.hr.slare;

import jakarta.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("share")
@ResponseBody
public class ShareController {

    //第一种方式：自动装配
    @Autowired
    private ServletContext context;

    //第二种方式：原生api
    public void data(HttpServletResponse response, HttpServletRequest request, HttpSession session){
        request.setAttribute("key","value");
    }

    @RequestMapping("data1")
    @ResponseBody
    public void data1(Map map){
       map.put("key","value");
    }

    @RequestMapping("data2")
    @ResponseBody
    public void data2(Model model){
       model.addAttribute("key","value");
    }

    @RequestMapping("data3")
    @ResponseBody
    public ModelAndView modelAndView(){
        //创建视图对象
        ModelAndView modelAndView =new ModelAndView();
        //往视图对象中添加值
        modelAndView.addObject("key","value");
        //给视图对象添加视图名称
        modelAndView.setViewName("视图名称 ");
        //返回视图对象
        return modelAndView;
    }
}
